﻿using System;
using System.Web.UI;
using App_Code.Modules;
using Portal.Core;
using Portal.Core.Binding.FormBuilder;
using Portal.Data.Entity;
using Portal.Data.Webparts;

public partial class Modules_WebpartPage_EditPart : WebpartPageModule
{
    private WebpartControl _control;
    private Webpart _part;
    protected override string Action
    {
        get { return "editpart"; }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        object obj = FindByPrimaryKey(typeof(Webpart), "partid");

        if (!IsPostBack)
        {
            Session[GetType().FullName + "@pageid"] = Request["id"];
        }
        if (obj == null)
        {
            Message.Visible = true;
            ApplyChange.Enabled = ApplyChange.Visible = false;
            return;
        }

        _part = (Webpart)obj;
        _control = (WebpartControl)Page.LoadControl(_part.Definition().Path);
        _control.Info = _part;

        LoadCustomParts(_part.Definition().Editor);

        SwitchRenderControlByType(_control);
    }

    private void LoadCustomParts(string p)
    {
        if (p == null)
        {
            return;
        }
        string[] editors = p.Split(',');

        foreach (string editor in editors)
        {
            EditorPart e = null;
            try
            {
                e = Page.LoadControl(editor) as EditorPart;
            }
            catch
            {
                e = null;
            }
            if (e != null)
            {
                phContent.Controls.Add(e);
                e.InitEditor(_control.Config);
            }
        }
    }

    protected void ApplyChange_Click(object sender, EventArgs e)
    {
        foreach (Control c in phContent.Controls)
        {
            if (c is EditorPart)
            {
                ((EditorPart)c).ApplyChange(_control.Config);
            }
        }
        _part.SetConfig(_control.Config);
        _part.Update();

        ActionPage.newParams.Add("id", Session[GetType().FullName + "@pageid"]);
        Forward("save");
    }

    protected void SwitchRenderControlByType(WebpartControl control)
    {
        var type = control.Info.GetConfig().GetType();
        switch (type.Name)
        {
            case "WigetCustomConfig":
                RenderControlWigetCustomConfig();
                break;
        }
    }

    protected void RenderControlWigetCustomConfig()
    {
        var builder = new Builder();
        var ucTableForm = builder.FindControlRecursive(phContent, "frmWebParts");
        ucTableForm.Controls.RemoveAt(ucTableForm.Controls.Count - 1); //Remove literalControl </table>
        ucTableForm.Controls.Add(new LiteralControl("<tr><td></td><td><input type='button' id='btnWizardData' name='btnWizardData' value='Lấy dữ liệu'></td></tr>"));
        ucTableForm.Controls.Add(new LiteralControl("</table>"));
        phContent.Controls.Add(new LiteralControl("<div class='popups'><span class='bClose'><span>X</span></span></div>"));
        Page.ClientScript.RegisterClientScriptInclude("selective", CoreContext.absStaticFile + "Themes/JS/widgetCustom.js");
        //Page.ClientScript.RegisterClientScriptInclude("selective", ResolveUrl(@"Scripts\TestScript.js"));

        //http://localhost:62744/Web/admin_WebpartPage_WebpartPage_editpage_editpart.aspx?partid=86244&id=8370
        var script = "<script language='javascript'>var prefixIDForm = '" + ucTableForm.ClientID.Replace("_" + ucTableForm.ID, "") + "';</script>";
        Page.ClientScript.RegisterStartupScript(GetType(), "onload", script);
    }

    public void GetUserControls(ControlCollection controls)
    {
        foreach (Control ctl in controls)
        {
            if (ctl is UserControl)
            {
                Response.Write(ctl.UniqueID + "<br />");
                GetUserControls(ctl.Controls);
                // Do whatever.
            }

            if (ctl.Controls.Count > 0)
                GetUserControls(ctl.Controls);
        }
    }
}